home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Storage / ODStor.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  13.2 KB  |  533 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ODStor.cpp
  3.  
  4.     Contains:    Implementation of ODStorageSystem
  5.  
  6.     Owned by:    Vincent Lo
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <6>     5/24/96    jpa        1246074: SOM_CATCH --> SOM_TRY..SOM_ENDTRY
  13.          <5>      5/1/96    JA        1314704: Directly instantiate Bento classes
  14.                                     w/o going through ODNewObject.
  15.          <4>     4/23/96    TJ        Removed ODStorB.cpp
  16.          <2>     3/15/96    DM        1295410: create list iterators on stack
  17.                                     (avoid mem thrash during purge)
  18.  
  19.     To Do:
  20.         **** ERROR HANDLING!!!! ****
  21.     In Progress:
  22.         
  23. */
  24.  
  25. #define ODStorageSystem_Class_Source
  26. #define VARIABLE_MACROS
  27. #include <ODStor.xih>
  28.  
  29. #ifndef _EXCEPT_
  30. #include "Except.h"
  31. #endif
  32.  
  33. #ifndef _STORPRIV_
  34. #include "StorPriv.h"
  35. #endif
  36.  
  37. #ifndef SOM_ODPart_xh
  38. #include <Part.xh>
  39. #endif
  40.  
  41. #ifndef SOM_ODNameSpaceManager_xh
  42. #include <NmSpcMg.xh>
  43. #endif
  44.  
  45. #ifndef SOM_ODSession_xh
  46. #include <ODSessn.xh>
  47. #endif
  48.  
  49. #ifndef SOM_Module_OpenDoc_StdDefs_defined
  50. #include <StdDefs.xh>
  51. #endif
  52.  
  53. #ifndef SOM_ODTranslation_xh
  54. #include <Translt.xh>
  55. #endif
  56.  
  57. #ifndef _ISOSTR_
  58. #include "ISOStr.h"
  59. #endif
  60.  
  61. #ifndef SOM_ODBinding_xh
  62. #include <ODBindng.xh>
  63. #endif
  64.  
  65. #ifndef SOM_ODTypeList_xh
  66. #include <TypeList.xh>
  67. #endif
  68.  
  69. #ifndef SOM_ODPlatformTypeList_xh
  70. #include <PfTypLs.xh>
  71. #endif
  72.  
  73. #ifndef SOM_ODFileContainer_xh
  74. #include "FileCtr.xh"
  75. #endif
  76.  
  77. #ifndef SOM_ODEmbeddedContainer_xh
  78. #include "EmbedCtr.xh"
  79. #endif
  80.  
  81. #ifndef SOM_ODMemContainer_xh
  82. #include "MemCtr.xh"
  83. #endif
  84.  
  85. #ifndef _ODNEW_
  86. #include <ODNew.h>
  87. #endif
  88.  
  89. #ifndef _ODMemory_
  90. #include <ODMemory.h>
  91. #endif
  92.  
  93. #ifndef _UTILERRS_
  94. #include "UtilErrs.h"
  95. #endif
  96.  
  97. //#define STATICBENTO
  98. #ifndef STATICBENTO
  99.  
  100.     #ifndef _ODNEWOBJ_
  101.     #include "ODNewObj.h"
  102.     #endif
  103.     
  104.     #ifndef __STRING__
  105.     #include <string.h>        // Only for comparing ContainerType. Should be removed when we use DLL.
  106.     #endif
  107.  
  108. #else
  109.  
  110.     #ifndef SOM_ODFileContainer_xh
  111.     #include <FileCtr.xh>
  112.     #endif
  113.  
  114.     #ifndef SOM_ODEmbeddedContainer_xh
  115.     #include <EmbedCtr.xh>
  116.     #endif
  117.     
  118.     #ifndef SOM_ODMemContainer_xh
  119.     #include <MemCtr.xh>
  120.     #endif
  121.     
  122. #endif
  123.  
  124. //==============================================================================
  125. // Constants
  126. //==============================================================================
  127.  
  128.  
  129. //==============================================================================
  130. // Function Prototype
  131. //==============================================================================
  132.  
  133. #ifdef STATICBENTO
  134.  
  135. static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID);
  136. static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID);
  137. static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID);
  138.  
  139. #endif
  140.  
  141. #pragma segment ODStorageSystem
  142.  
  143. //#include "ODStorB.cpp"    // Platform-independent methods, if any
  144.  
  145. SOM_Scope ODMemoryHeapID  SOMLINK ODStorageSystemGetHeap(ODStorageSystem *somSelf, Environment *ev)
  146. {
  147.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  148.     ODStorageSystemMethodDebug("ODStorageSystem","GetHeap");
  149.  
  150.     return _fHeap;
  151. }
  152.  
  153. SOM_Scope void  SOMLINK ODStorageSystemsomUninit(ODStorageSystem *somSelf)
  154. {
  155.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  156.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemsomUninit");
  157.  
  158.     if (_fContainers)
  159.     {
  160.         TRY{
  161.             Environment* ev = somGetGlobalEnvironment();
  162.             
  163.             ContainerListIterator iter(_fContainers);
  164.             iter.Initialize();
  165.         
  166.             ODContainer*    container = iter.Last();
  167.             while (container != kODNULL) {
  168.                 container->Close(ev);
  169.                 delete container;
  170.                 container = iter.Previous();
  171.             }
  172.             // delete iter;
  173.             
  174.             delete _fContainers;
  175.         }CATCH_ALL{
  176.         }ENDTRY
  177.     }
  178. }
  179.  
  180. SOM_Scope ODSize  SOMLINK ODStorageSystemPurge(ODStorageSystem *somSelf, Environment *ev,
  181.         ODSize size)
  182. {
  183.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  184.     ODStorageSystemMethodDebug("ODStorageSystem","Purge");
  185.  
  186.     ODULong sizePurged = 0; ODVolatile( sizePurged );
  187.     
  188.     SOM_TRY
  189.     
  190.         ContainerListIterator iter(_fContainers);
  191.         iter.Initialize();
  192.     
  193.         ODContainer*    container = iter.Last();
  194.         while (container != kODNULL) {
  195.             TRY
  196.                 sizePurged += container->Purge(ev, size-sizePurged);
  197.             CATCH_ALL
  198.             ENDTRY
  199.             // dh - I did't wrap the iterator call in a TRY block, because there is no
  200.             // use continuing with the iterator if there is a problem with it.
  201.             container = iter.Previous();
  202.         }
  203.         // delete iter;
  204.  
  205.         sizePurged += parent_Purge(somSelf,ev,size);
  206.     SOM_CATCH_ALL
  207.         WARN("Error %ld trying to purge in ODStorageSystemPurge",ErrorCode());
  208.         SetErrorCode(kODNoError);        // dh - Eat the exception; Purge should not 
  209.                                         // propagate it because clients function
  210.                                         // fine whether memory was purged or not.
  211.     SOM_ENDTRY
  212.     
  213.     return sizePurged;
  214. }
  215.  
  216. SOM_Scope void  SOMLINK ODStorageSystemInitStorageSystem(ODStorageSystem *somSelf, Environment *ev,
  217.         ODSession* session)
  218. {
  219.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  220.     ODStorageSystemMethodDebug("ODStorageSystem","InitStorageSystem");
  221.  
  222.     SOM_TRY
  223.     
  224.     /* Moved from somInit. SOM itself sets fields to zero
  225.     _fSession = kODNULL;
  226.     _fHeap = kDefaultHeapID;
  227.     _fContainers = kODNULL;
  228.     */
  229.     _fSession = session;
  230.     _fHeap = ODRecoverHeapID(somSelf);
  231.     
  232.     _fContainers = new(somSelf->GetHeap(ev)) ContainerList;
  233.     _fContainers->Initialize();
  234.     
  235.     SOM_CATCH_ALL
  236.     SOM_ENDTRY
  237. }
  238.  
  239. SOM_Scope ODSession*  SOMLINK ODStorageSystemGetSession(ODStorageSystem *somSelf, Environment *ev)
  240. {
  241.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  242.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGetSession");
  243.  
  244.     return _fSession;
  245. }
  246.  
  247. SOM_Scope ODContainer*  SOMLINK ODStorageSystemAcquireContainer(ODStorageSystem *somSelf, Environment *ev,
  248.         ODContainerType containerType,
  249.         ODContainerID* id)
  250. {
  251.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  252.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemAcquireContainer");
  253.     
  254.     SOM_TRY
  255.  
  256.     ODContainer*    container = kODNULL;
  257.     
  258.     if ((container = _fContainers->Get(id)) != kODNULL) {
  259.         container->Acquire(ev);
  260.     }
  261.     else {
  262. #ifndef STATICBENTO
  263.  
  264.         ODContainerSuite theContainerSuite = 
  265.             _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
  266.         
  267.         if ( theContainerSuite )
  268.         {
  269.             TRY
  270.                 container = (ODContainer*) ODNewObject(theContainerSuite);
  271.             CATCH_ALL
  272.                 ODDisposePtr( theContainerSuite );
  273.                 THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
  274.                                                            :ErrorCode() );
  275.             ENDTRY
  276.             ODDisposePtr( theContainerSuite );
  277.         }
  278.         else
  279.             THROW( kODErrCannotCreateContainer );
  280.             
  281. #else
  282.         // Hardwire for ODContainer for now. We should do the dynamic loading
  283.         //    of the class using containerType in the future.
  284.  
  285.         if (strcmp(kODDefaultFileContainer, containerType) == 0)
  286.             container = NewODFileContainer(somSelf->GetHeap(ev));
  287.         else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
  288.             container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
  289.         else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
  290.             container = NewODMemContainer(somSelf->GetHeap(ev));
  291.             
  292. #endif            
  293.         container->InitContainer(ev, somSelf, id);
  294.         
  295.         _fContainers->Add(container);
  296.         
  297.         container->Open(ev);
  298.     }
  299.     return container;
  300.  
  301.     SOM_CATCH_ALL
  302.     SOM_ENDTRY
  303.     return kODNULL;
  304. }
  305.  
  306. SOM_Scope ODContainer*  SOMLINK ODStorageSystemCreateContainer(ODStorageSystem *somSelf, Environment *ev,
  307.         ODContainerType containerType,
  308.         ODContainerID* id)
  309. {
  310.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  311.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateContainer");
  312.  
  313.     SOM_TRY
  314.  
  315.     ODContainer*    container = kODNULL;
  316.     
  317.     if ((container = _fContainers->Get(id)) != kODNULL)
  318.         THROW(kODErrContainerExists);
  319.     
  320. #ifndef STATICBENTO
  321.  
  322.     // Hardwire the Bento classnames for speed.
  323.     if (strcmp(kODBentoFileContainer, containerType) == 0)
  324.         container = new ODFileContainer;
  325.     else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
  326.             container = new ODEmbeddedContainer;
  327.     else if (strcmp(kODBentoMemoryContainer, containerType) == 0)
  328.             container = new ODMemContainer;
  329.     else {
  330.     
  331.         // General case:
  332.         ODContainerSuite theContainerSuite = 
  333.             _fSession->GetBinding(ev)->GetContainerSuite(ev,containerType);
  334.         
  335.         if ( theContainerSuite )
  336.         {
  337.             TRY
  338.                 container = (ODContainer*) ODNewObject(theContainerSuite);
  339.             CATCH_ALL
  340.                 ODDisposePtr( theContainerSuite );
  341.                 THROW( ErrorCode()==kODErrCantLoadSOMClass ?kODErrCannotCreateContainer
  342.                                                            :ErrorCode() );
  343.             ENDTRY
  344.             ODDisposePtr( theContainerSuite );
  345.         }
  346.         else
  347.             THROW( kODErrCannotCreateContainer );
  348.     }
  349.     
  350.     if( !container )
  351.         THROW( kODErrCannotCreateContainer );
  352.     
  353. #else
  354.     // Hardwire for the two Bento Containers for now. We should do the dynamic loading
  355.     //    of the class using containerType in the future.
  356.  
  357.     if (strcmp(kODDefaultFileContainer, containerType) == 0)
  358.         container = NewODFileContainer(somSelf->GetHeap(ev));
  359.     else if (strcmp(kODBentoEmbeddedContainer, containerType) == 0)
  360.             container = NewODEmbeddedContainer(somSelf->GetHeap(ev));
  361.     else if (strcmp(kODDefaultMemoryContainer, containerType) == 0)
  362.             container = NewODMemContainer(somSelf->GetHeap(ev));
  363.             
  364. #endif
  365.  
  366.     container->InitContainer(ev, somSelf, id);
  367.     
  368.     _fContainers->Add(container);
  369.     
  370.     container->Create(ev);
  371.     container->Open(ev);
  372.  
  373.     return container;
  374.  
  375.     SOM_CATCH_ALL
  376.     SOM_ENDTRY
  377.     return kODNULL;
  378. }
  379.  
  380. SOM_Scope void  SOMLINK ODStorageSystemNeedSpace(ODStorageSystem *somSelf, Environment *ev,
  381.         ODSize memSize,
  382.         ODBoolean doPurge)
  383. {
  384.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  385.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
  386.  
  387.     if (doPurge != kODFalse) {
  388.         SOM_TRY
  389.         somSelf->Purge(ev, memSize);
  390.         SOM_CATCH_ALL
  391.         SOM_ENDTRY
  392.     }
  393. }
  394.  
  395. SOM_Scope ODTypeList*  SOMLINK ODStorageSystemCreateTypeList(ODStorageSystem *somSelf, Environment *ev,
  396.         ODTypeList* typeList)
  397. {
  398.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  399.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemCreateTypeList");
  400.  
  401.     ODTypeList* tmp; ODVolatile(tmp);
  402.     
  403.     SOM_TRY
  404.     
  405.     tmp = new ODTypeList;
  406.     THROW_IF_NULL(tmp);
  407.     tmp->InitTypeList(ev, typeList);
  408.     
  409.     SOM_CATCH_ALL
  410.         ODDeleteObject(tmp);
  411.     SOM_ENDTRY
  412.     return tmp;
  413. }
  414.  
  415. SOM_Scope ODPlatformTypeList*  SOMLINK ODStorageSystemCreatePlatformTypeList(ODStorageSystem *somSelf, Environment *ev,
  416.         ODPlatformTypeList* typeList)
  417. {
  418.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  419.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemNeedSpace");
  420.     
  421.     ODPlatformTypeList* tmp;
  422.     
  423.     SOM_TRY
  424.  
  425.     tmp = new ODPlatformTypeList;
  426.     if (tmp == kODNULL)
  427.         ODSetSOMException(ev, kODErrOutOfMemory);
  428.     else
  429.         tmp->InitPlatformTypeList(ev, typeList);
  430.         
  431.     SOM_CATCH_ALL
  432.         tmp = kODNULL;
  433.     SOM_ENDTRY
  434.  
  435.     
  436.     return tmp;
  437. }
  438.  
  439.  
  440. SOM_Scope void  SOMLINK ODStorageSystemGrowHeap(ODStorageSystem *somSelf, Environment *ev,
  441.         ODSize memSize)
  442. {
  443.     //ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  444.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemGrowHeap");
  445.  
  446. ODUnused(memSize);
  447. }
  448.  
  449. SOM_Scope ODStorageSystem*  SOMLINK ODStorageSystemReleaseContainer(ODStorageSystem *somSelf, Environment *ev,
  450.         ODContainer* container)
  451. {
  452.     ODStorageSystemData *somThis = ODStorageSystemGetData(somSelf);
  453.     ODStorageSystemMethodDebug("ODStorageSystem","ODStorageSystemReleaseContainer");
  454.  
  455.     SOM_TRY
  456.  
  457.     ODContainerID containerID = container->GetID(ev);
  458.         
  459.     if ((container = _fContainers->Get(&containerID)) != kODNULL) {
  460.         _fContainers->Remove(&containerID);
  461.         ODDisposePtr(containerID._buffer);
  462.         TRY
  463.             container->Close(ev);
  464.             delete container;
  465.         CATCH_ALL
  466.             SetErrorCode(kODNoError);
  467.         ENDTRY
  468.     }
  469.     else
  470.     {
  471.         ODDisposePtr(containerID._buffer);
  472.         THROW(kODErrContainerDoesNotExist);
  473.     }
  474.         
  475.     return somSelf;
  476.  
  477.     SOM_CATCH_ALL
  478.     SOM_ENDTRY
  479.     return somSelf;
  480. }
  481.  
  482. #ifdef STATICBENTO
  483.  
  484. //------------------------------------------------------------------------------
  485. // NewODFileContainer
  486. //------------------------------------------------------------------------------
  487.  
  488. static ODFileContainer* NewODFileContainer(ODMemoryHeapID heapID)
  489. {
  490.     SOMClass*    fileCtrClass = ODFileContainerNewClass(ODFileContainer_MajorVersion,
  491.                                                         ODFileContainer_MinorVersion);
  492.     ODULong        size = fileCtrClass->somGetInstanceSize();
  493.     ODPtr        buffer = ODNewPtr(size, heapID);
  494.     ODFileContainer*    fileCtr = (ODFileContainer*) fileCtrClass->somRenew(buffer);
  495.     
  496.     return fileCtr;
  497. }
  498.  
  499. //------------------------------------------------------------------------------
  500. // NewODMemContainer
  501. //------------------------------------------------------------------------------
  502.  
  503. static ODMemContainer* NewODMemContainer(ODMemoryHeapID heapID)
  504. {
  505.     SOMClass*    memCtrClass = ODMemContainerNewClass(ODMemContainer_MajorVersion,
  506.                                                         ODMemContainer_MinorVersion);
  507.     ODULong        size = memCtrClass->somGetInstanceSize();
  508.     ODPtr        buffer = ODNewPtr(size, heapID);
  509.     ODMemContainer*    memCtr = (ODMemContainer*) memCtrClass->somRenew(buffer);
  510.     
  511.     return memCtr;
  512. }
  513.  
  514. //------------------------------------------------------------------------------
  515. // NewODEmbeddedContainer
  516. //------------------------------------------------------------------------------
  517.  
  518. static ODEmbeddedContainer* NewODEmbeddedContainer(ODMemoryHeapID heapID)
  519. {
  520.     SOMClass*    embeddedCtrClass = ODEmbeddedContainerNewClass(ODEmbeddedContainer_MajorVersion,
  521.                                                                 ODEmbeddedContainer_MinorVersion);
  522.     ODULong        size = embeddedCtrClass->somGetInstanceSize();
  523.     ODPtr        buffer = ODNewPtr(size, heapID);
  524.     ODEmbeddedContainer*    embeddedCtr = (ODEmbeddedContainer*) embeddedCtrClass->somRenew(buffer);
  525.     
  526.     return embeddedCtr;
  527. }
  528.  
  529. #endif
  530.  
  531.  
  532.  
  533.